//
//  XGRegisterViewModel.swift
//  TodayNews
//
//  Created by monkey on 2020/4/12.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import LeanCloud

protocol XGLeanCloudErrorReason
{
    func reasonFromErrorCode(code: Int) -> String?
}

extension XGLeanCloudErrorReason
{
    func reasonFromErrorCode(code: Int) -> String?
    {
        switch code {
        case 210:
            return "用户名密码不匹配!"
        case 211:
            return "用户名不存在!"
        case 214:
            return "手机号已被注册!"
        default:
            return nil
        }
    }
}

class XGRegisterViewModel: XGUserAccountValidateProtocol, XGLeanCloudErrorReason
{
    
    /// 注册用户
    /// - Parameters:
    ///   - screenName: 用户昵称
    ///   - phoneNumber: 手机号
    ///   - email: 邮箱
    ///   - password: 密码
    ///   - confirmPassword: 确认密码
    ///   - completion: 完成回调
    func registerUser(screenName: String, phoneNumber: String, email: String,password: String, confirmPassword: String,completion: @escaping (Bool,String) -> Void)
    {
        if !validateUserName(userName: screenName) {
            completion(false, "用户名长度应设置在3~20位!")
        } else if !validatePhoneNumber(phone: phoneNumber) {
            completion(false, "手机号码格式不正确!")
        } else if !validateEmail(email: email) {
            completion(false, "邮箱格式不正确!")
        } else if password != confirmPassword {
            completion(false, "两次密码输入不一致!")
        } else if !validatePassword(password: password) {
            completion(false, "密码长度应设置在6~18位!")
        } else {
            let user = XGUserModel()
            user.screenName = LCString(screenName)
            user.username = LCString(phoneNumber)
            user.password = LCString(password)
            user.isClientAccount = LCBool(true)
            user.email = LCString(email)
            user.mobilePhoneNumber = LCString("+86" + phoneNumber)
            user.signUp { (result) in
                result.isSuccess ? completion(true, "注册成功!,请返回登录!") : completion(false, self.reasonFromErrorCode(code: result.error?.code ?? 0) ?? "注册失败!")
            }
        }
    }
}

protocol XGUserAccountValidateProtocol
{
    
    /// 验证用户名是否合法 3-20位字符
    /// - Parameter userName: 用户名
    func validateUserName(userName: String) -> Bool
    
    /// 验证手机号是否合法
    /// - Parameter phone: 手机号
    func validatePhoneNumber(phone: String) -> Bool
    
    /// 验证密码是否合法
    /// - Parameter password: 6-18位字符
    func validatePassword(password: String) -> Bool
    
    /// 验证邮箱是否合法
    /// - Parameter email: 邮箱
    func validateEmail(email: String) -> Bool
}

extension XGUserAccountValidateProtocol
{
    func validateUserName(userName: String) -> Bool
    {
        let regex = "^.{3,20}$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: userName)
    }
    
    func validatePhoneNumber(phone: String) -> Bool
    {
        let regex = #"^[1][3-8]\d{9}$"#
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: phone)
    }
    
    func validatePassword(password: String) -> Bool
    {
        let regex = "^.{6,18}$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: password)
    }
    
    func validateEmail(email: String) -> Bool {
        let regex = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: email)
    }
}
